package com.example.myspringframework.aop.framework.adapter;

import com.example.myspringframework.aop.BeforeAdvice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
// 这样的类有4中，分别是方法调用之前、之后、返回前、抛异常时
public class MethodBeforeAdviceInterceptor implements MethodInterceptor {
    private BeforeAdvice advice;

    public MethodBeforeAdviceInterceptor() {
    }

    public MethodBeforeAdviceInterceptor(BeforeAdvice advice) {
        this.advice = advice;
    }

    public void setAdvice(BeforeAdvice advice) {
        this.advice = advice;
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        this.advice.before(methodInvocation.getMethod(), methodInvocation.getArguments(), methodInvocation.getThis());
        return methodInvocation.proceed();
    }
}
